<template>
  <div class="container-xxl flex-grow-1 container-p-y">
    <el-select
      v-model="fileType"
      @change="handleFileTypeChange"
      placeholder="文件类型"
      clearable
      size="small"
      style="width: 240px"
    >
      <el-option
        v-for="dict in fileTypeOptions"
        :key="dict.dictValue"
        :label="dict.dictLabel"
        :value="dict.dictValue"
      />
    </el-select>
    <el-row>&nbsp;</el-row>
    <el-upload
      ref="upload"
      :limit="1"
      accept=".html"
      :headers="upload.headers"
      :action="upload.url + '?updateSupport=' + upload.updateSupport"
      :disabled="upload.isUploading"
      :on-progress="handleFileUploadProgress"
      :on-success="handleFileSuccess"
      :auto-upload="false"
      drag
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处，或
        <em>点击上传</em>
      </div>
      <div class="el-upload__tip" style="color:red" slot="tip">提示：只允许html文件！</div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">确 定</el-button>
      <el-button @click="upload.open = false">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { getDicts } from "@/api/system/dict/data";

export default {
  name: "DocUpload",
  data() {
    return {
      // 用户导入参数
      fileTypeOptions: [],
      fileType: null,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/uploadApiDoc"
      },
    };
  },
  mounted() {
    getDicts("upload_file_type").then(response => {
      this.fileTypeOptions  = response.data;
    });
  },
  beforeDestroy: function () {
  },
  methods: {
    handleFileTypeChange() {
      this.upload.url = process.env.VUE_APP_BASE_API + "/common/uploadApiDoc/" + this.fileType;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 20px 20px 5px 20px;
  border-style: solid;
  border-width: 0px 0 2px 0;
  border-color: whitesmoke;
}

::v-deep .el-dialog__footer {
  padding: 12px 20px;
}

@media (max-width: 480px) {
  ::v-deep .el-dialog__body {
    padding: 20px 20px 5px 20px;
    border-style: solid;
    border-width: 0px 0 2px 0;
    border-color: whitesmoke;
  }
  ::v-deep .el-dialog__footer {
    padding: 12px 20px;
  }
  ::v-deep .el-pagination__sizes {
    margin: 0;
  }
  ::v-deep .el-pagination .el-select .el-input {
    width: 85px;
    margin: 0;
  }
  ::v-deep .el-pagination__total {
    margin-right: 2px;
  }
  ::v-deep .el-pagination__jump {
    display: none !important;
  }
}
</style>
